// component/business/business.js
import API from "../../api/index";
import valid from "../../utils/validate";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    categoriesIndex:{type: Number,value: 2}, // 1 案例 2 咨询
    navIndex: {
      type: Number,
      value: 1
    } // 在详情中 线上/线下用到
  },
  /**
   * 组件的初始数据
   */
  data: {
    categories: [],
    form: {
      name: '',
      phone: '',
      textarea: ''
    },
    bind: false // 用来判断按钮
  },
  /**
   * 组件的方法列表
   */
  ready() { //组件布局完成，这时可以获取节点信息，也可以操作节点
    this._getData()
  },
  methods: {
    _getData() { //获取线上/线下案例分类
      API.getCategories(this.data.categoriesIndex).then(res => {
        if (res.code == 200) {
          let arr = res.data.categories;
          let newArr = []
          arr.forEach(e => {
            e.child_category.forEach(q => {
              delete q.cate_logo
              q.select = false
            })
            newArr.push(e.child_category)
          })
          console.log(newArr)
          this.setData({ categories: newArr })
        } else {
          this.setData({ categories: [] })
        }
      })
    },
    getInput(e) { // 获取输入内容
      const value = e.detail.value;
      switch (Number(e.currentTarget.dataset.index)) {
        case 0:
          this.setData({
            ['form.name']: value
          });
          break;
        case 1:
          this.setData({
            ['form.phone']: value
          });
          break;
        default:
          this.setData({
            ['form.textarea']: value
          });
          break;
      }
      this.setData({ bind: typeof this.checkValue() != 'string' })
    },
    changeBusiness(e) {
      let i = e.currentTarget.dataset.index;
      let pi = this.data.categoriesIndex!=2? this.data.navIndex : 0
      this.setData({
        [`categories[${pi}][${i}].select`]: !this.data.categories[pi][i].select
      })
      this.setData({bind: typeof this.checkValue() != 'string'})
    },
    checkValue() {
      let data = this.data.form;
      let title = true;
      let arr = this.data.categories[this.data.categoriesIndex!=2 ? this.data.navIndex : 0];
      let newArr = []
      for (let i in arr) {
        if (arr[i].select) {
          newArr.push(i)
        }
      }
      console.log(newArr)
      if (!newArr.length) {
        title = '请选择咨询类型'
      } else if (data.name.replace(/\s/g, "") == '') {
        title = '请输入姓名'
      } else if (data.phone.length == 0) {
        title = '请输入手机号'
      } else if (!valid.isMobile(data.phone)) {
        title = '请输入正确的手机号'
      } else if (data.textarea.replace(/\s/g, "") == '') {
        title = '请填写留言信息'
      } else {
        title = newArr
      }
      return title
    },
    sendData() {
      const d = this.data.form;
      let title = '';
      if (typeof this.checkValue() == 'string') {
        title = this.checkValue()
      } else {
        API.getConsults(this.checkValue(), d.name, d.phone, d.textarea).then(res => {
          if (res.code == 200) {
            wx.showToast({ title: res.data.msg })
            this.triggerEvent('goConsult')
          } else {
            wx.showToast({title: res.data.msg,icon: 'none'})
          }
        })
      }
      if (title) {
        wx.showToast({title,icon: 'none'})
        this.setData({bind: false})
      }
    }
  }
})